home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / kafsort.zip / SORTEST.C < prev    next >
Text File  |  1993-05-28  |  4KB  |  157 lines

  1. /********************************************************************
  2.  * SORTEST.C
  3.  ********************************************************************/
  4. #include <stdio.h>
  5. #include <conio.h>
  6.  
  7. /**********************************************
  8.  * Define Values For Sort Operations
  9.  **********************************************/
  10. #define OK    0
  11. #define TRUE  1
  12. #define FALSE 0
  13. #define FETCHEND   -1
  14.  
  15. FILE *fst, *ftd;
  16.  
  17. long fetch();
  18. extern int sorterror;    /* Error Code for Sort */
  19.  
  20. void main();
  21. void asorterr();
  22.  
  23. void main()
  24. {
  25.    char line[165];
  26.    char sorttag[31];
  27.    char *lptr, *sptr;
  28.    int itemcount, sortcount;
  29.    long pos;
  30.  
  31.    clrscr();
  32.    itemcount=0;
  33.    printf("                 Sort Test Program\n\n");
  34.    printf("     LISTS ARE DISPLAYED WITH DELAYED SCROLLING\n\n");
  35.    printf("This Sort Test builds a TEST file from the 'SORTEST.C' source file\n");
  36.    printf("if the test file (STEST.DAT) does not exist in the current directory.\n");
  37.    printf("BE SURE the 'SORTEST.C' file or the 'STEST.DAT' file exists in\n");
  38.    printf("the current directory. For test purposes Spaces are being replaced\n");
  39.    printf("with a  character to better visualize the sort process.\n");
  40.    printf("\n\nPRESS ANY KEY TO BEGIN THE TEST -or- <ESC> to quit ...");
  41.    if(getch() == 27) exit(0);
  42.    clrscr();
  43.  
  44.    if((fst = fopen("STEST.DAT", "rb"))==NULL)
  45.       {
  46.    printf("Building Sort Test File ...\n\n");
  47.  
  48.    if((fst = fopen("STEST.DAT", "wb"))==NULL)
  49.       {
  50.       printf("$$ Error Creating SORT TEST File $$");
  51.       exit(0);
  52.       }
  53.    if((ftd = fopen("SORTEST.C", "rt"))==NULL)
  54.       {
  55.       printf("$$ Error Opening SORTTEST.C File $$");
  56.       exit(0);
  57.       }
  58.    while(fgets(line, 160, ftd)!=NULL)
  59.       {
  60.       strcpy(sorttag,"");
  61.       sptr=sorttag;
  62.       lptr=line;
  63.       while(*lptr == ' ') lptr++;  /* skip Leading Spaces */
  64.       while(*lptr)
  65.         {
  66.         if(*lptr == '\n') break;
  67.         *sptr++ = *lptr++;
  68.         }
  69.       fwrite(sorttag,20,1,fst); /* Truncate the Line to 1st 20 chars only */
  70.       itemcount++;
  71.       }
  72.    fclose(ftd);
  73.    printf("\n%d Items Were Written\n",itemcount);
  74.     }
  75.  
  76.    fclose(fst);
  77.    printf("\n### Press <ENTER> to view UNSORTED LIST ###\n");
  78.    while(getch()!=13);
  79.  
  80.    if((fst = fopen("STEST.DAT", "rb"))==NULL)
  81.       {
  82.       printf("$$ Error Opening SORT TEST File $$");
  83.       exit(0);
  84.       }
  85.    while(fread(line,20,1,fst)==1)
  86.       {
  87.       line[20]=0;
  88.       printf("%s\n",line);
  89.       delay(70);
  90.       }
  91.  
  92.  
  93.    fclose(fst);
  94.    printf("\n>>>> Press <ENTER> to SORT LIST <<<<\n");
  95.    while(getch()!=13);
  96.    clrscr();
  97.  
  98.  
  99.    if((fst = fopen("STEST.DAT", "rb"))==NULL)
  100.       {
  101.       printf("$$ Error Opening SORT TEST File $$");
  102.       exit(0);
  103.       }
  104.  
  105.    printf("\nSORT IN PROGRESS ...\n");
  106.    if(initsort(20)!=OK)  asorterr();  /* Init the sort */
  107.  
  108.    sortcount = 0;
  109.    pos = 0;
  110.    while(fread(line,20,1,fst)==1)
  111.       {
  112.       line[20]=0;
  113.       if(sort(line,pos) != OK) asorterr();
  114.       pos = ftell(fst); /* Get file position */
  115.       sortcount++;
  116.       }
  117.    printf("SORT COMPLETED - %d Records Passed To Sort\n",sortcount);
  118.  
  119.    printf("MERGE IN PROGRESS ...\n");
  120.    if(merge()!=OK) asorterr();
  121.    printf("MERGE COMPLETED --\n");
  122.    printf("FETCH IN PROGRESS ...\n");
  123.  
  124.    printf("\n>>>> NOW -- Press <ENTER> to view SORTED LIST <<<<\n");
  125.    while(getch()!=13);
  126.    clrscr();
  127.  
  128.    while(TRUE)
  129.       {
  130.       pos = fetch();
  131.       if(pos == FETCHEND) break;  /* End of sorted Records */
  132.       if(pos < 0) asorterr();
  133.       if(fseek(fst,pos,0))
  134.        {
  135.        printf("\n$$ ERROR SEEKING TO RECORD at %ld $$\n",pos);
  136.        exit(0);
  137.        }
  138.       if(fread(line, 20, 1, fst))  /* Read a record */
  139.        {
  140.        line[20]=0;
  141.        printf("%s\n",line);
  142.        delay(100);
  143.        }
  144.       }
  145.  fclose(fst);
  146.  printf("***** SORT TEST CONCLUDED *****\n");
  147. }
  148.  
  149. /**************************
  150.  * ASORTERR - A sorting Error Occured
  151.  *****************************/
  152. void asorterr()
  153. {
  154.    printf("\n$$$ A SORT ERROR - ERROR CODE = %d $$$\n", sorterror);
  155.    exit(0);
  156. }
  157.